<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class informasi extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_informasi');
		//$this->load->library('Excel_generator');
		//$this->output->enable_profiler(TRUE);
	}
	
	function index(){
		$this->load->view('portal/backend/informasi');
	}
	
	function grid(){
		$data = $this->mdl_informasi->getdata();
		echo $this->mdl_informasi->togrid($data['row_data'], $data['row_count']);
	}
	
	function initCKEditor($id){
		return array(
		
			//ID of the textarea that will be replaced
			'id' 	=> 	$id,
			'path'	=>	'asset/js/ckeditor',
		
			//Optionnal values
			'config' => array(
				'toolbar' 	=> 	"Full", 	//Using the Full toolbar
				'width' 	=> 	"100%",	//Setting a custom width
				'height' 	=> 	'100px',	//Setting a custom height
			   	'filebrowserBrowseUrl' => base_url().'/asset/js/kcfinder/browse.php?type=files',
			   	'filebrowserImageBrowseUrl' => base_url().'/asset/js/kcfinder/browse.php?type=images',
			   	'filebrowserFlashBrowseUrl' => base_url().'/asset/js/kcfinder/browse.php?type=flash',
			   	'filebrowserUploadUrl' => base_url().'/asset/js/kcfinder/upload.php?type=files',
			   	'filebrowserImageUploadUrl' => base_url().'/asset/js/kcfinder/upload.php?type=images',
			   	'filebrowserFlashUploadUrl' => base_url().'/asset/js/kcfinder/upload.php?type=flash'
			),
		
		);
	}
	
	function add(){		
		$data['content_id'] = '';
		$data['category_id'] = '';
		$data['content_title'] = '';
		$data['content'] = '';
		$data['summary'] = '';
		$data['url'] = '';
		$data['date_post'] = date('d/m/Y');
		$data['published'] = '';
		$this->load->view('portal/backend/informasi_form', $data);
	}
	
	function edit($kode){
		$r = $this->mdl_informasi->getdataedit($kode);
		
		$data['content_id'] = $kode;
		$data['category_id'] = $r->row()->category_id;
		$data['content_title'] = $r->row()->content_title;
		$data['content'] = $r->row()->content;
		$data['summary'] = $r->row()->summary;
		$data['url'] = $r->row()->url;
		$data['date_post'] = FormatDateFromMysql($r->row()->date_post);
		$data['published'] = $r->row()->published;
		
		$this->load->view('portal/backend/informasi_form', $data);
	}
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		# rules validasi form
		$this->form_validation->set_rules("category_id", 'Kategori', 'trim|required|xss_clean');
		$this->form_validation->set_rules("content_title", 'Judul Konten', 'trim|required|xss_clean');
		$this->form_validation->set_rules("content", 'Konten', 'trim|required|xss_clean');
		$this->form_validation->set_rules("published", 'Status Publikasi', 'trim|required|xss_clean');
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_fakultas',' ',' '))==''?'':form_error('kd_fakultas',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			if($aksi=="add"){ // add
					$result = $this->mdl_informasi->InsertOnDb($data);
			}else { // edit				
					$result=$this->mdl_informasi->UpdateOnDb($data);
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete($kode){
		$result = $this->mdl_informasi->DeleteOnDb($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}
}